package entity;

import java.util.Objects;

/**
 * 重写equals()和hashCode()方法只要key相等就代表其对象相同
 * remove()、add()、contains()等方法都会满足业务需求
 */
public class ExpireKey implements Comparable<ExpireKey>{
    private String key;
    private Long timeStamp;

    public ExpireKey(String key) {
        this.key = key;
    }

    public ExpireKey(String key, Long timeStamp) {
        this.key = key;
        this.timeStamp = timeStamp;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Long timeStamp) {
        this.timeStamp = timeStamp;
    }

    @Override
    public int hashCode(){
        return Objects.hash(key);
    }

    @Override
    public boolean equals(Object obj){
        if (this==obj)return true;
        if (obj==null||this.getClass()!=obj.getClass())return false;
        ExpireKey other=(ExpireKey) obj;
        return Objects.equals(key,other.key);
    }

    @Override
    public String toString(){
        return "{"+key+(timeStamp==null?"":","+timeStamp)+"}";
    }

    @Override
    public int compareTo(ExpireKey o) {
        return timeStamp.compareTo(o.timeStamp);
    }
}
